home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / jcl / jcl_long.cpp < prev    next >
C/C++ Source or Header  |  1999-12-09  |  2KB  |  90 lines

  1. // $Id: jcl_long.cpp,v 1.2 1999/12/09 18:02:26 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include <iostream.h>
  11. #include "jcl_long.h"
  12.  
  13. void BaseLong::divide(BaseLong dividend, BaseLong divisor, BaseLong "ient, BaseLong &remainder)
  14. {
  15.     u4 high = dividend.high_word(),
  16.        low  = dividend.low_word(),
  17.        remainder_high = 0;
  18.  
  19.     int i;
  20.     for (i = 0; i < 32; i++)
  21.     {
  22.         remainder_high = (remainder_high << 1) | (high >> 31);
  23.         high <<= 1;
  24.         if ((ULong) divisor <= remainder_high)
  25.         {
  26.             high++;
  27.             remainder_high -= divisor.low_word();
  28.         }
  29.     }
  30.  
  31.     remainder = BaseLong(0, remainder_high);
  32.  
  33.     for (i = 0; i < 32; i++)
  34.     {
  35.         remainder <<= 1;
  36.         remainder.low_word() |= (low >> 31);
  37.         low <<= 1;
  38.         if ((ULong) divisor <= remainder)
  39.         {
  40.             low++;
  41.             remainder -= divisor;
  42.         }
  43.     }
  44.  
  45.     quotient = BaseLong(high, low);
  46.  
  47.     return;
  48. }
  49.  
  50.  
  51. void ULong::String(char *result)
  52. {
  53.     ULong value = *this;
  54.     char *ptr = result;
  55.  
  56.     do
  57.     {
  58.         *ptr++ = '0' + (value % 10).low_word();
  59.         value /= 10;
  60.     } while (value != 0);
  61.  
  62.     *ptr = '\0';
  63.  
  64.     for (char *tail = ptr - 1; tail > result; tail--, result++)
  65.     {
  66.         char c = *tail;
  67.         *tail = *result;
  68.         *result = c;
  69.     }
  70.  
  71.     return;
  72. }
  73.  
  74.  
  75. void Long::String(char *result)
  76. {
  77.     ULong value;
  78.  
  79.     if (high_word() & 0x80000000)
  80.     {
  81.         *result++ = '-';
  82.         value = -(*this);
  83.     }
  84.     else value = *this;
  85.  
  86.     value.String(result);
  87.  
  88.     return;
  89. }
  90.